package com.cos.zuul.filer;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: COS
 * @time: 2022/4/29 19:27
 * @description:
 */
@Component
public class TestRouteFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        //做路由 页面的跳转
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+5;//10
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("触发动态路径");
        //要跳转 设置1参数 作为标识 跳转到那个页面
        // 获取reqeust
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String method = request.getParameter("method");
        System.out.println("method=======>"+method);
        if (method!=null&&method.equals("ver")) {
            //http://localhost:8079/con/getVersion
            //http://localhost/conosumer-v2/con/getVersion
            //设置要去哪个服务 设置服务id=>服务名
            currentContext.put(FilterConstants.SERVICE_ID_KEY,"conosumer-v2");
            //填写服务后面写的路径/con/getVersion
            currentContext.put(FilterConstants.REQUEST_URI_KEY,"/con/getVersion");
        }else  if (method!=null&&method.equals("rou")) {
//http://localhost:8079/con/getVersion
            //http://localhost/conosumer-v2/con/getRouter
            //设置要去哪个服务 设置服务id=>服务名
            currentContext.put(FilterConstants.SERVICE_ID_KEY,"conosumer-v2");
            //填写服务后面写的路径/con/getRouter
            currentContext.put(FilterConstants.REQUEST_URI_KEY,"/con/getRouter");
        }
        return null;
    }
}
